package ru.ssau.customerManagement.controller.threads.client;

import ru.ssau.customerManagement.controller.remoting.OperationRequest;
import ru.ssau.customerManagement.controller.remoting.OperationResponse;
import ru.ssau.customerManagement.controller.remoting.RequestExecutor;
import ru.ssau.customerManagement.controller.remoting.notifications.ClientSideNotificationController;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.Socket;

/**
 * Created by illabefat on 27.11.14.
 */
public class NotificationsListener implements Runnable{
    private final Socket socketToListen;
    private final ClientSideNotificationController notificationController;
    private final RequestExecutor requestExecutor;

    public NotificationsListener(Socket socketToListen, ClientSideNotificationController notificationController) {
        this.socketToListen = socketToListen;
        this.notificationController = notificationController;
        requestExecutor = new RequestExecutor(this.notificationController);
    }

    @Override
    public void run() {
        try {
            ObjectOutputStream out = new ObjectOutputStream(socketToListen.getOutputStream());
            ObjectInputStream in = new ObjectInputStream(socketToListen.getInputStream());
            while (true)
            {
                OperationRequest request = (OperationRequest) in.readObject();
                OperationResponse response = requestExecutor.executeRequest(request);
                out.writeObject(response);
                out.flush();
                out.reset();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
